package monster.comm.key;
import java.util.*;
/**
 * 自动生成一组序列号，是单一模式的运用
 * <p>Title: 系统框架设计</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author 蒋增奎
 * @version 1.0
 */
public class KeyGenerator {
  private static KeyGenerator keygen=new KeyGenerator();
  private static final int POOL_SIZE=100;  //在内存里面保存多少
  private static HashMap keyList=new HashMap();//存在多 个主键
  private  KeyGenerator() {

  }

  /**
   * 得到实例
   * @return
   */
 public static KeyGenerator getInstance(){

   return keygen;
 }

 /**
  * 得到一个唯一值
  * @param name
  * @return
  */
 protected synchronized int getKey(String name){
   KeyInfo keyinfo;
   if(keyList.containsKey(name)){
     keyinfo=(KeyInfo)keyList.get(name);
   }
   else{
     keyinfo=new KeyInfo(this.POOL_SIZE,name);
     keyList.put(name,keyinfo);
   }
    return keyinfo.getNextKey();
 }

}
